package com.example.gpstracking;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Activity;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidGPSTrackingActivity extends Activity {
	
	Button btnShowLocation;
	GPSTracker gps;
	/*protected LocationManager locationManager;	
	Location location; 
	double latitude; // latitude
	double longitude; // longitude
	GPSTracker gps;*/
	/*public Location getLocation() {
		try {
			locationManager = (LocationManager) getBaseContext()
					.getSystemService(LOCATION_SERVICE);

			isGPSEnabled = locationManager
					.isProviderEnabled(LocationManager.GPS_PROVIDER);

			isNetworkEnabled = locationManager
					.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

			if (!isGPSEnabled && !isNetworkEnabled) {
			} else {
				this.canGetLocation = true;
				if (isNetworkEnabled) {
					locationManager.requestLocationUpdates(
							LocationManager.NETWORK_PROVIDER,
							MIN_TIME_BW_UPDATES,
							MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
					Log.d("Network", "Network");
					if (locationManager != null) {
						location = locationManager
								.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if (location != null) {
							latitude = location.getLatitude();
							longitude = location.getLongitude();
						}
					}
				}
				if (isGPSEnabled) {
					if (location == null) {
						locationManager.requestLocationUpdates(
								LocationManager.GPS_PROVIDER,
								MIN_TIME_BW_UPDATES,
								MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
						Log.d("GPS Enabled", "GPS Enabled");
						if (locationManager != null) {
							location = locationManager
									.getLastKnownLocation(LocationManager.GPS_PROVIDER);
							if (location != null) {
								latitude = location.getLatitude();
								longitude = location.getLongitude();
							}
						}
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return location;
	}*/
	 static GoogleMap map,_map; LatLng latLng;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   /*	 map = ((MapFragment) getFragmentManager()
				.findFragmentById(R.id.map)).getMap();
	 map.setPadding(0, 50, 0, 100);*/
	
        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
        
        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {	
				//Location locationManage2r=getLocation();
				//Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + locationManage2r.getLatitude() + "\nLong: " + locationManage2r.getLongitude(), Toast.LENGTH_LONG).show();
			//	new File_().addToMap(String.valueOf(locationManage2r.getLatitude()), String.valueOf(locationManage2r.getLongitude()), 0);
		        gps = new GPSTracker(AndroidGPSTrackingActivity.this);
		        if(gps.canGetLocation()){
		        	
		        	double latitude = gps.getLatitude();
		        	double longitude = gps.getLongitude();int qq;
		        	//praca
		        	final NumberFormat df = DecimalFormat.getInstance();
		        	df.setMinimumFractionDigits(2);
		        	df.setMaximumFractionDigits(4);
		        	df.format(latitude);
		        	String a=df.toString();
		        	  LatLng latLng = new LatLng(latitude, longitude);
		        	  map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		        	//map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		        	MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude))
		    				.title("aaa");
		    		map.addMarker(marker);
		    		CameraUpdate up = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude),
		    				16);
		    		map.animateCamera(up);
		        	//if(a=="51.0983")
		        	//	Toast.makeText(getApplicationContext(), "Praca" + longitude, Toast.LENGTH_LONG).show();
		        	//Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
		        	//new File_().addToMap(String.valueOf(latitude), String.valueOf(longitude), 0);
		        }else{
		        	gps.showSettingsAlert();
		        }
				
			}
		});
    }
    
}